extends layout.pug

block content
  -var actionName = update ? "更新" : "发布";
  div
    h3= actionName+"新文章"
    div.row(style="")
      div.col-sm-8
        label(for="top", style="margin-right: 5px;")= "置顶"
        input(type="checkbox" id="top" checked=defaultTop)
    div.row(style="margin-bottom: 10px;")
      div.col-sm-8
        div.input-group
          div.input-group-addon
            = "标题"
          input#title.form-control(placeholder="标题", value=defaultTitle)
      div.col-sm-4
        div.input-group
          div.input-group-addon
            = "标签"
          input#tags.form-control(placeholder="标签，多个标签用 \",\" 分隔，如 a,b", value=defaultTags)
    div#editor(style="min-height: 380px;")
    p.text-right(style="margin-top: 10px;")
      button.btn.btn-success#deploy()= actionName

  script.
      var actionName = '#{actionName}';
      var action = '#{update ? "update" : "deploy"}';
      var editor = new wangEditor('editor');
      var tagDOM = $('#tags');
      var titleDOM = $('#title');
      tagDOM.val("!{defaultTags}" || localStorage.getItem("editor_tags") || "");
      titleDOM.val('!{defaultTitle}' || localStorage.getItem("editor_title") || "");
      tagDOM.on('input', function () {
          localStorage.setItem('editor_tags', tagDOM.val());
      })
      titleDOM.on('input', function () {
          localStorage.setItem('editor_title', titleDOM.val());
      })
      editor.config.uploadImgUrl = '/api/upload';
      var emoji = !{JSON.stringify(emoji)};
      //$.get("/api/emotions", function (json) {
          var data = {};
          for (var title in emoji) {
              data[title] = {
                  title: title,
                  data: emoji[title]
              }
          }

          editor.config.emotions = data;
          editor.create();
          var html = `!{defaultContent}` || localStorage.getItem('editor_html') || '<p>请输入内容</p>'
          editor.$txt.html(html);
          editor.onchange = function () {
              localStorage.setItem('editor_html', this.$txt.html());
          }
          $('#deploy').click(function () {
              var content = editor.$txt.html();
              var title = titleDOM.val().trim();
              var tag = tagDOM.val().trim();
              if (!title || !content) {
                  $.moyuAlert("请输入标题和正文内容");
                  return;
              }
              var tags = tag.split(",");
              var self = $(this);

              $.moyuConfirm("确定" + actionName + "?", function () {
                  self.prop('disabled', true);
                  $.post("/api/post", {
                      action: action,
                      top: $('#top').prop('checked'),
                      title: title,
                      content: content,
                      tags: tags,
                      id: '#{update}'
                  }, function (data) {
                      if (data.code == 200) {
                          $.moyuAlert(actionName + "成功");
                      } else {
                          $.moyuAlert("很遗憾，" + actionName + "失败");
                      }
                      self.prop('disabled', false);
                  }, 'json');
              })

          });

      //}, 'json');

